<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Session;
use \think\Db;
class Formuser extends Common
{
	public function adduser(){

		$citylist = Db::name('city')->where('city_status',1)->order('city_id')->select();
		$this->assign('citylist',$citylist);
		return $this->fetch();
	}

  public function edituser(){
    $userinfo = Db::name('user')->where('user_id',input('userid'))->find();
    $citylist = Db::name('city')->where('city_status',1)->order('city_id')->select();
    $this->assign('user',$userinfo);
    $this->assign('citylist',$citylist);
    return $this->fetch();
  }


	public function add(){
		$info = '';
       	if (request()->isPost()) {
       		

       		//创建数组，用video表的所有字段赋值
          if(input('param.loginname')==''||input('param.loginpassword')==''||input('param.gender')==''||input('param.tel')==''||input('param.username')==''||input('param.intro')==''){
            $this->error("请填写完整信息",'formuser/adduser');
          }
          $result = Db::name('user')->where('user_loginname',input('param.loginname'))->find();
          $result2 = Db::name('user')->where('user_email',input('param.tel'))->find();
          $result3 = Db::name('user')->where('user_name',input('param.username'))->find();
          if($result){
             $this->error("账户名重复",'formuser/adduser');
          }
          if($result2){
          	$this->error("邮箱重复",'formuser/adduser');
          }
          if($result3){
          	$this->error("用户昵称重复",'formuser/adduser');
          }
          $file = request()->file('pic');
          $data=array();
          if($file){
              $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
              $data=[
                  'user_name'=>input('param.username'),
                  'user_loginname'=>input('param.loginname'),
                  'user_password'=>md5(input('param.loginpassword')),
                  'user_gender'=>input('param.gender'),
                  'user_email'=>input('param.tel'),
                  'city_id'=>input('city'),
                  'user_intro'=>input('param.intro'),
                  'user_regtime'=>date('Y-m-d H:i:s'),
                  'user_iswechat'=>0,
                  'user_wechatcode'=>'',
                  'user_headimage'=>$info->getFilename(),
       		];
          }else{
          	$data=[
                  'user_name'=>input('param.username'),
                  'user_loginname'=>input('param.loginname'),
                  'user_password'=>md5(input('param.loginpassword')),
                  'user_gender'=>input('param.gender'),
                  'user_email'=>input('param.tel'),
                  'city_id'=>input('city'),
                  'user_intro'=>input('param.intro'),
                  'user_regtime'=>date('Y-m-d H:i:s'),
                  'user_iswechat'=>0,
                  'user_wechatcode'=>'',
                  'user_headimage'=>'default.jpg',
       		];
          }
        $res=Db::name('user')->insert($data);
        
        //处理结果
        if ($res) {
        	$this->success("添加用户成功",'user/index');
        }
        else {
        	$this->error("添加用户失败",'user/index');
        }
       	}
	}

  public function edit(){
   $info = '';
        if (request()->isPost()) {
          $id = input('userid');
          $result1 = Db::name('user')->where('user_email',input('param.tel'))->find();
          $result2 = Db::name('user')->where('user_name',input('param.username'))->find();
          if($result1&&$result1['user_id']!=$id){
            $this->error("联系方式重复",'formuser/edituser');
          }
          if($result2&&$result2['user_id']!=$id){
            $this->error("用户昵称重复",'formuser/edituser');
          }
          $file = request()->file('pic');
          $pic="";
          $pic2=input('pic2');//图片没改之前的名字
          $data=array();
          if($file){
              $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'static'. DS .'posters');
              if($info){
                $pic = $info->getFilename();
                 if(input('param.loginpassword')==''){
          $data=[
                  'user_name'=>input('param.username'),
                  'user_gender'=>input('param.gender'),
                  'user_email'=>input('param.tel'),
                  'city_id'=>input('city'),
                  'user_intro'=>input('param.intro'),
                  'user_headimage'=>$info->getFilename(),
          ];
        }else{
          $data=[
                  'user_name'=>input('param.username'),
                  'user_loginname'=>input('param.loginname'),
                  'user_password'=>md5(input('param.loginpassword')),
                  'user_gender'=>input('param.gender'),
                  'user_email'=>input('param.tel'),
                  'city_id'=>input('city'),
                  'user_intro'=>input('param.intro'),
                  'user_headimage'=>$info->getFilename(),
          ];
        }
              }else{
                echo $file->getError();
              }
          }
          else{
          if(input('param.loginpassword')==''){
          $data=[
                  'user_name'=>input('param.username'),
                  'user_gender'=>input('param.gender'),
                  'user_email'=>input('param.tel'),
                  'city_id'=>input('city'),
                  'user_intro'=>input('param.intro'),
          ];
        }else{
          $data=[
                  'user_name'=>input('param.username'),
                  'user_loginname'=>input('param.loginname'),
                  'user_password'=>md5(input('param.loginpassword')),
                  'user_gender'=>input('param.gender'),
                  'user_email'=>input('param.tel'),
                  'city_id'=>input('city'),
                  'user_intro'=>input('param.intro'),
          ];
        }
}
          
      
          if(!empty($pic)&&$pic2!='default.jpg'){
            if(file_exists(ROOT_PATH . 'public' . DS . 'static'. DS .'posters'.DS.$pic2)){
              unlink(ROOT_PATH . 'public' . DS . 'static'. DS .'posters'.DS.$pic2);
            }
          }
          
         
        $res=Db::name('user')->where("user_id",$id)->update($data);
        //处理结果
        if ($res!==false) {
          $this->success("修改用户成功",'user/index');
        }
        else {
          $this->error("修改用户失败",'user/index');
        }
      }
	
}

  public function delete(){
    $res = Db::name('user')->where('user_id',input('userid'))->update(['user_isdel'=>input('statu')]);
    if($res===false){
        $this->error("处理失败",'user/index');
    }
    $this->redirect('user/index');
  }
}